package com.sunny.ynhz.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.sunny.ynhz.R;
import com.sunny.ynhz.api.NetUrl;
import com.sunny.ynhz.ui.ImageActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luao on 2017/7/15.
 */

public class ImageViewAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    ImageViewAdapter imageViewAdapter = this;
    private ArrayList imagePathArr;

    public interface delImageListener {
        void onClickDelBtn(int i);
    }

    private delImageListener delImageListener;

    public void setDelImageListener(delImageListener delImage) {
        this.delImageListener = delImage;
    }


    public ImageViewAdapter(@Nullable List<String> data) {
        super(R.layout.item_image, data);
        data.add("image");
        imagePathArr = new ArrayList();
    }


    @Override
    protected void convert(final BaseViewHolder helper, final String item) {
//        helper.getView(R.id.del_image).setVisibility(View.GONE);
        //判断是不是来源于服务器图片
        String imageUrl = "";
        if (item.indexOf("FileUploadpath") != -1) {
            imageUrl = NetUrl.imageBaseUrl + item;
        } else {
            imageUrl = item;
        }

        if (helper.getLayoutPosition() < mData.size() - 1) {
            helper.getView(R.id.del_image).setVisibility(View.VISIBLE);

            Log.e("image", "imageUrl ========== " + mData.toString());
            Glide.with(mContext)
                    .load(imageUrl)
                    .error(R.mipmap.xwdt)
                    .centerCrop()
                    .into((ImageView) helper.getView(R.id.image));
        } else {
            helper.getView(R.id.del_image).setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(R.mipmap.icon_add_image)
                    .centerCrop()
                    .into((ImageView) helper.getView(R.id.image));
        }
        Log.e("image", "" + mData.size());
        final String finalImageUrl = imageUrl;
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (helper.getLayoutPosition() == mData.size() - 1) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(mContext);
                    intent.setSelectModel(SelectModel.MULTI);
                    intent.setShowCarema(true); // 是否显示拍照， 默认false
                    intent.setMaxTotal(9); // 最多选择照片数量，默认为9
                    imagePathArr.clear();
                    imagePathArr.addAll(mData);
                    imagePathArr.remove(0);
                    Log.e("image", "imageUrl =====            ===== " + imagePathArr.toString());
                    intent.setSelectedPaths(imagePathArr); // 已选中的照片地址， 用于回显选中状态
                    ((Activity) mContext).startActivityForResult(intent, 0);
                } else {
                    Intent intent = new Intent(mContext, ImageActivity.class);
                    intent.putExtra("pic_path", finalImageUrl);
                    if (Build.VERSION.SDK_INT < 21) {
                        mContext.startActivity(intent);
                    } else {
                        ActivityOptionsCompat options = ActivityOptionsCompat.
                                makeSceneTransitionAnimation((Activity) mContext, view, "shareAnimTexView");
                        mContext.startActivity(intent, options.toBundle());
                    }
                }

            }
        });

        helper.getView(R.id.del_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mData.remove(helper.getLayoutPosition());
                imageViewAdapter.notifyDataSetChanged();
                Log.e("image", "imageUrl ========== " + mData.toString());
//                if (delImageListener != null) {
//                    helper.getView(R.id.del_image).setVisibility(View.VISIBLE);
//                }
                delImageListener.onClickDelBtn(helper.getLayoutPosition());
            }
        });

    }
}
